import { request } from '@/utils/request'
import type {
  KnowledgeParams,
  KnowledgePage,
  PageParams,
  DoctorPage,
  FollowType,
  TopDep,
  ConsultOrderPreParams,
  ConsultOrderPreData,
  PartialConsult,
  ConsultOrderItem,
  ConsultOrderListParams,
  ConsultOrderPage
} from '@/types/consult'
import type { Patient } from '@/types/user'

// 获取文章列表接口 => 拿返回值(添加泛型)
export const getKnowledgeList = (obj: KnowledgeParams) =>
  request<KnowledgePage>('/patient/home/knowledge', 'GET', obj)

export const getDoctorPage = (params: PageParams) =>
  request<DoctorPage>('/home/page/doc', 'GET', params)

// followOrUnfollow('1')  关注id为1的医生
// followOrUnfollow('1')  取消关注id为1的医生
// followOrUnfollow('1', 'knowledge')  关注id为1的文章
// followOrUnfollow('1', 'knowledge')  取消关注id为1的文章
// followOrUnfollow('10', 'topic')  关注id为10的话题
// followOrUnfollow('10', 'topic')  取消关注id为10的话题
// 不需要获取返回值 也就不要设置泛型!!!
export const followOrUnfollow = (id: string, type: FollowType = 'doc') =>
  request('/like', 'POST', { id, type })

export const getAllDep = () => request<TopDep[]>('/dep/all')

// HTMLImageElement 内置的图片元素类型
// File 内置的文件类型
export const uploadImage = (file: File) => {
  // const obj = {}
  // obj.file = file

  // formdata 一种类型的数据而已
  const fm = new FormData()
  fm.append('file', file)

  return request<{
    id: string
    url: string
  }>('/upload', 'POST', fm)
}

// 拉取订单信息
export const getConsultOrderPre = (params: ConsultOrderPreParams) =>
  request<ConsultOrderPreData>('/patient/consult/order/pre', 'GET', params)

// 查询患者详情
export const getPatientDetail = (id: string) =>
  request<Patient>(`/patient/info/${id}`)

// 生成问诊订单
// 生成订单
export const createConsultOrder = (data: PartialConsult) =>
  request<{ id: string }>('/patient/consult/order', 'POST', data)

// 获取支付地址  0 是微信  1 支付宝
export const getConsultOrderPayUrl = (params: {
  paymentMethod: 0 | 1
  orderId: string
  payCallback: string // 支付成功后跳转回项目中哪个页面
}) => request<{ payUrl: string }>('/patient/consult/pay', 'POST', params)
// payUrl => 得到一个支付地址(包含支付金额 个人信息 加密)

// 获取订单详情(订单状态 订单倒计时)
export const getConsultOrderDetail = (orderId: string) =>
  request<ConsultOrderItem>('/patient/consult/order/detail', 'GET', { orderId })

// 查看处方
export const getPrescriptionPic = (id: string) =>
  request<{ url: string }>(`/patient/consult/prescription/${id}`)

// 评价问诊
export const evaluateConsultOrder = (data: {
  docId: string
  orderId: string
  score: number
  content: string
  anonymousFlag: 0 | 1
}) => request<{ id: string }>('/patient/order/evaluate', 'POST', data)

export const getConsultOrderList = (params: ConsultOrderListParams) =>
  request<ConsultOrderPage>('/patient/consult/order/list', 'GET', params)

// 取消订单 cancelOrder('1')
export const cancelOrder = (id: string) =>
  request(`/patient/order/cancel/${id}`, 'PUT')

// 删除订单
export const deleteOrder = (id: string) =>
  request(`/patient/order/${id}`, 'DELETE')
